import { history } from 'umi';
import { queryPayWay, queryOrderPay, queryPayInfo, queryPayResult, queryTestPayResult } from '@/services/order';
import { isEmpty } from '@/utils/common';
import { Message } from '@/design';

const cardList = ['cardpay'];
let spinning = false;

export default {
  namespace: 'payment',

  state: {
    payInfo: {},
    payway: [],
    payhold: {}
  },

  effects: {
    /* 查詢支付方式 */
    *fetchPayWay({ payload: { diningWay } }: any, { put, call, select }: any) {
      console.log('查詢支付方式', diningWay)
      let response = yield call(queryPayWay, { diningWay });
      if (isEmpty(response)) return false;
      if (isEmpty(response.data)) {
        Message.error('請聯繫門店管理員開通支付渠道');
        // history.replace('/order');
        return false;
      }
      yield put({
        type: 'savePayWay',
        payload: response.data || []
      })
    },
    /* 查詢待支付訂單信息 */
    *fetchPayInfo({ payload }: any, { put, call }: any) {
      console.log('sadasdsa', payload)
      let response = yield call(queryPayInfo, payload);
      if (isEmpty(response)) return false;
      // const { braId, merId, orderNo = '', orderPayAmt: price = 0, timeOut = 0 } = response.data;
      yield put({
        type: 'savePayInfo',
        payload: response.data
      })
      yield put({
        type: 'fetchPayway',
      })
      // if (isEmpty(response.data.timeOut) || response.data.timeOut < 1) {
      //   history.replace('/order');
      // }
    },
    /* 訂單支付 */
    *fetchOrderPay({ payload }: any, { put, call }: any) {
      if (spinning) {
        return false;
      }
      spinning = true;
      let response = yield call(queryOrderPay, payload);
      spinning = false;
      if (isEmpty(response) || isEmpty(response.data)) {
        Message.error('支付異常，請稍後重試')
        // history.replace('/order');
        return false;
      };
      /* 如果成功 */
      yield put({
        type: 'savePayhold',
        payload: response.data || {}
      })
      /* 刷卡支付 */
      if (cardList.indexOf(response.data.payWay) > -1) {
        if (isEmpty(response.data.payUrl)) {
          Message.success('支付成功')
          history.replace(`/payment-result`);
        } else {
          history.replace('/payment-wait');
        }
      } else {//'applepay', 'linepay'
        /* 非刷卡支付 */
        if (!isEmpty(response.data.payUrl)) {
          history.replace(`/payment-wait/${response.data.lineOrderNo}?orderId=${payload.orderId}`);
          document.location.href = response.data.payUrl;
        }
      }
    },
    /* 查詢待支付訂單信息 */
    *fetchPayResult({ payload, paymentback, timed } : any, { put, call }:any) {
      let response = yield call(queryPayResult, payload);
      if (isEmpty(response)) return false;
      // if (response.code === '100') {
      //   paymentback && paymentback();
      // }
      if (response.code === 200 && response.data != 100) {
        clearInterval(timed);
        history.replace(`/payment-result`);
        yield put({
          type: 'savePayhold',
          payload: response.data || {}
        })
      }
      if (response.code === 400) {
        Message.error('支付失敗');
        clearInterval(timed);
        history.replace(`/orderdetail?orderNo=${payload.orderId}`);
      }
      // if (response.data === 'success') {
      //   clearInterval(timed);
      //   history.replace(`/payment-result`);
      // }
      // if (response.data === 'failed') {
      //   Message.error('支付失敗');
      //   clearInterval(timed);
      //   history.replace(`/order`);
      // }
      // if (response.data === 'cancel') {
      //   Message.error('支付取消');
      //   clearInterval(timed);
      //   history.replace(`/order`);
      // }

    },
  },

  reducers: {
    savePayInfo(state: any, { payload }: any) {
      return {
        ...state,
        payInfo: payload
      }
    },
    savePayWay(state: any, { payload }: any) {
      return {
        ...state,
        payway: payload
      }
    },
    savePayhold(state: any, { payload }: any) {
      return {
        ...state,
        payhold: payload
      }
    }
  }

}